import org.gradle.api.DefaultTask
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.mybatis.generator.api.MyBatisGenerator
import org.mybatis.generator.config.Configuration
import org.mybatis.generator.config.xml.ConfigurationParser
import org.mybatis.generator.internal.DefaultShellCallback
import java.io.File


public abstract class MybatisTask : DefaultTask() {

    @get:Input
    lateinit var path: Provider<String>

    @TaskAction
    fun genMybatisMapper() {
        println("genMybatisMapper starter")
        val warnings: List<String> = ArrayList()
        val overwrite = true
        val configFile: File = File(path.get())
        val cp = ConfigurationParser(warnings)
        val config: Configuration = cp.parseConfiguration(configFile)
        val callback = DefaultShellCallback(overwrite)
        val myBatisGenerator = MyBatisGenerator(config, callback, warnings)
        myBatisGenerator.generate(null)

        println("genMybatisMapper end")
    }
}
